﻿using Architecture.Repository.Interface.Traceability;

namespace Traceability.Domain.Service.PictureService
{
    public class PictureService : IPictureService
    {
        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository _picturesRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="picturesRepository">图片仓储</param>
        public PictureService(IPictureRepository picturesRepository)
        {
            _picturesRepository = picturesRepository;
        }

        /// <summary>
        /// 添加图片信息
        /// </summary>
        /// <param name="pictures">图片实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddPicture(Pictures pictures)
        {
            return await _picturesRepository.AddAsync(pictures);
        }

        /// <summary>
        /// 获取图片列表
        /// </summary>
        /// <returns>返回图片列表信息</returns>
        public async Task<List<Pictures>> GetPictures()
        {
            return await _picturesRepository.GetAsync(x=>!x.IsDeleted);
        }

        /// <summary>
        /// 删除图片信息
        /// </summary>
        /// <param name="pictureId">图片ID</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public async Task<int> DeletedPicture(int pictureId, string? userName)
        {
            var res = await _picturesRepository.GetModelAsync(pictureId);
            res.IsDeleted = true;
            res.DeletedBy = userName;
            res.DeletedDate = DateTime.Now;
            return await _picturesRepository.UpdateAsync(res);
        }

        /// <summary>
        /// 反填图片实体
        /// </summary>
        /// <param name="pictureId">图片ID</param>
        /// <returns>返回图片实体信息</returns>
        public async Task<Pictures> GetPictureModel(int pictureId)
        {
            return await _picturesRepository.GetModelAsync(pictureId);
        }

        /// <summary>
        /// 修改图片信息
        /// </summary>
        /// <param name="pictures">图片实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> UpdatePicture(Pictures pictures)
        {
            return await _picturesRepository.UpdateAsync(pictures);
        }
    }
}
